//    Copyright (C) Kherty.  All rights reserved.
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace OpenLS.UI
{
    public class HighlightElement:UIElement
    {


        public static bool  GetIsHighlighted(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsHighlightedProperty);
        }

        public static void SetIsHighlighted(DependencyObject obj, bool value)
        {
            obj.SetValue(IsHighlightedProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsHighlighted.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsHighlightedProperty =
            DependencyProperty.RegisterAttached("IsHighlighted", typeof(bool), typeof(HighlightElement), new UIPropertyMetadata(false, OnIsHighlightedChanged));

        static  void OnIsHighlightedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Control c = (Control) sender;
            string contentProperty = null;
            if (c is ContentControl)
                contentProperty = "Content";
            else if (c is HeaderedItemsControl)
                contentProperty = "Header";
            bool b = (bool) e.NewValue;
            FrameworkElement ic = ItemsControl.ItemsControlFromItemContainer(c);
            if (ic == null)
                return;
            if (b)
            {
                if (contentProperty != null)
                {
                    Binding binding = new Binding(contentProperty);
                    SetHighlightedElement(ic, c);
                    binding.Source = c;
                    ic.SetBinding(HighlightedItemProperty, binding);
                    ic.RaiseEvent(new RoutedEventArgs(HighlightedItemChangedEvent, ic));
                }
            }
            else
            {
                if (GetHighlightedElement(ic) == c)
                {
                    SetHighlightedElement(ic, null);
                    SetHighlightedItem(ic, null);
                    ic.RaiseEvent(new RoutedEventArgs(HighlightedItemChangedEvent, ic));
                }
            }
        }
        public static readonly RoutedEvent HighlightedItemChangedEvent = EventManager.RegisterRoutedEvent("HighlightedItemChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HighlightElement));
        [Category("Behavior")]
        public event RoutedEventHandler HighlightedItemChanged
        {
            add
            {
                AddHandler(HighlightedItemChangedEvent, value);
            }
            remove
            {
                RemoveHandler(HighlightedItemChangedEvent, value);
            }
        }
 
 

        public static object GetHighlightedItem(DependencyObject obj)
        {
            return obj.GetValue(HighlightedItemProperty);
        }

        public static void SetHighlightedItem(DependencyObject obj, object value)
        {
            obj.SetValue(HighlightedItemProperty, value);
        }

        // Using a DependencyProperty as the backing store for HighlightedItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HighlightedItemProperty =
            DependencyProperty.RegisterAttached("HighlightedItem", typeof(object), typeof(HighlightElement), new UIPropertyMetadata(null));



        public static FrameworkElement GetHighlightedElement(DependencyObject obj)
        {
            return (FrameworkElement)obj.GetValue(HighlightedElementProperty);
        }

        public static void SetHighlightedElement(DependencyObject obj, FrameworkElement value)
        {
            obj.SetValue(HighlightedElementProperty, value);
        }

        // Using a DependencyProperty as the backing store for HighlightedElement.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HighlightedElementProperty =
            DependencyProperty.RegisterAttached("HighlightedElement", typeof(FrameworkElement), typeof(HighlightElement), new UIPropertyMetadata(null));




    }
}
